package com.qf.controller;

import com.github.pagehelper.PageInfo;
import com.qf.pojo.Game;
import com.qf.pojo.QueryParams;
import com.qf.pojo.Role;
import com.qf.service.GameService;
import com.qf.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.validation.Valid;
import java.util.List;

@Controller
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleService roleService;
    
    @Autowired
    private GameService gameService;

    //分页查询
    @GetMapping("/list")
    public String list(QueryParams params,String deleteInfo,
                       @RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum,
                       @RequestParam(value = "pageSize",defaultValue = "2")Integer pageSize,
                       Model model){
        model.addAttribute("deleteInfo",deleteInfo);
        PageInfo<Role> pageInfo = roleService.findRoleAll(params, pageNum, pageSize);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("params",params);

        List<Game> gameList = gameService.findGameAll();
        model.addAttribute("gameList",gameList);

        return "index";
    }

    //跳转到添加页面
    @GetMapping("/insert-ui")
    public String insertUI(String insertInfo,Model model){
        model.addAttribute("insertInfo",insertInfo);
        List<Game> gameList = gameService.findGameAll();
        model.addAttribute("gameList",gameList);

        return "insert";
    }

    //执行添加
    @PostMapping("/insert")
    public String insert(@Valid Role role, BindingResult bindingResult, RedirectAttributes redirectAttributes) throws Exception {
        if(bindingResult.hasErrors()){
            String message = bindingResult.getFieldError().getDefaultMessage();
            redirectAttributes.addAttribute("insertInfo",message);
            return "redirect:/role/insert-ui";
        }
        Integer count = roleService.addRole(role);
        if(count==1){
            return "redirect:/role/list";
        }else {
            return "redirect:/role/insert-ui";
        }
    }

    //跳转到修改页面，进行回显
    @GetMapping("/update-ui/{roleId}")
    public String updateUI(@PathVariable Integer roleId,String updateInfo,Model model){
        model.addAttribute("updateInfo",updateInfo);
        model.addAttribute("roleId",roleId);
        List<Game> gameList = gameService.findGameAll();
        model.addAttribute("gameList",gameList);
        Role list = roleService.findRoleOne(roleId);
        model.addAttribute("list",list);

        return "update";
    }

    //执行修改
    @PostMapping("/update")
    public String update(@Valid Role role, BindingResult bindingResult,RedirectAttributes redirectAttributes){
//        Integer roleId = role.getRoleId();
//        if(bindingResult.hasErrors()){
//            String message = bindingResult.getFieldError().getDefaultMessage();
//            redirectAttributes.addAttribute("updateInfo",message);
//            return "redirect:/role/update-ui?roleId="+roleId;
//        }
        Integer count = roleService.updateRole(role);
//        if(count==1){g
            return "redirect:/role/list";
//        }else {
//            redirectAttributes.addAttribute("updateInfo","修改失败!");
//            return "redirect:/role/update-ui?roleId="+roleId;
//        }
    }

    //执行删除
    @GetMapping("/delete/{roleId}")
    public String delete(@PathVariable Integer roleId,RedirectAttributes redirectAttributes){
        Integer count = roleService.deleteRole(roleId);
        if(count==1){
            return "redirect:/role/list";
        }else {
            redirectAttributes.addAttribute("deleteInfo","删除失败!");
            return "redirect:/role/list";
        }

    }

}
